import { Menu,Icon,Tag } from 'antd'
import { connect } from 'react-redux'
import React,{ Component } from 'react'
import { Link } from 'react-router-dom'
import { Logout } from '../actions/home'

const SubMenu = Menu.SubMenu;

class Navbar extends Component{
  timerID = null

  constructor(props){
    super(props)
    this.state = { time: new Date()}
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      time: new Date()
    });
  }

  handleClick = (e) => {
    const { dispatch } = this.props
    if(e.key === 'close'){
      dispatch(Logout())
    }
  }

  render(){
    return (
      <Menu
      onClick={this.handleClick}
      theme='dark'
      mode='horizontal'
      >
      <Menu.Item><Link to='/'><Icon type='home'/></Link></Menu.Item>
      <SubMenu title={<span><Icon type="appstore" /></span>}>
      <Menu.Item><Link to='/disk'><Icon type='hdd'/>网盘</Link></Menu.Item>
      </SubMenu>
      <Menu.Item key='close'><span><Icon type='poweroff'/></span></Menu.Item>
      <Menu.Item><Tag color='black'>{this.state.time.toLocaleTimeString()}</Tag></Menu.Item>
      </Menu>
    )
  }
}

const mapStateToProps = (state) => {
  return {
    home: state.home
  }
}

export default connect(mapStateToProps)(Navbar)
